package com.www.applyword.ui.chat;

import android.content.Context;
import android.text.TextUtils;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.DynamicAdapter2;
import com.www.applyword.adapter.FansImagesAdpter;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.DynaminDetailsBean;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;
import com.www.applyword.view.MyGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2019/11/11 0011 10:15
 * version: 1.0
 */
public class DynamicDetailsActivity extends BaseActivity {
    @BindView(R.id.civ_personal_head_image)
    CircleImageView civPersonalHeadImage;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_dynamic_detail)
    TextView tvDynamicDetail;
    @BindView(R.id.rv_dynamic_photos)
    MyGridView rvDynamicPhotos;
    @BindView(R.id.rv_new_comment)
    RecyclerView rv_new_comment;
    @BindView(R.id.et_send_comment)
    EditText etSendComment;
    @BindView(R.id.sb_comment)
    Button sbComment;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;
    @BindView(R.id.iv_like)
    ImageView ivLike;
    @BindView(R.id.tv_like)
    TextView tvLike;
    @BindView(R.id.ll_like)
    LinearLayout llLike;
    @BindView(R.id.iv_msg)
    ImageView ivMsg;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    private int page = 1;
    private int Rows = 50;

    private DynamicAdapter2 mDynamicAdapter;
    private String forumID;
    public static InputMethodManager inputMethodManager;
    private List<DynaminDetailsBean.ForumCommentBeanX> forumCommentBeanXList = new ArrayList<>();
    private List<Boolean> alreadyLikeList = new ArrayList<>();
    private DynaminDetailsBean dynaminDetailsBean;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.dynamin_details_activity);
        setTitle("论坛详情");
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    protected void loadData() {
        forumID = getIntent().getStringExtra("ForumID");
        rv_new_comment.setLayoutManager(new LinearLayoutManager(this));
        rv_new_comment.setItemAnimator(new DefaultItemAnimator());
        mDynamicAdapter = new DynamicAdapter2(this, forumID, etSendComment);
        rv_new_comment.setAdapter(mDynamicAdapter);
        refresh.setOnRefreshListener(refreshLayout -> getForumInformation(true));

        refresh.setOnLoadMoreListener(refreshLayout -> getForumInformation(false));
        getForumInformation(true);
    }

    private void getForumInformation(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("ForumID", forumID);
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        HttpClient.get(DynamicDetailsActivity.this, Constants.GETFORUMINFORMATION, params, new CallBack<DynaminDetailsBean>() {
            @Override
            public void onSuccess(DynaminDetailsBean result) {
                dynaminDetailsBean = result;
                if (isRefresh) {
                    refresh.finishRefresh(true);
                    mDynamicAdapter.replaceAll(result.getForumComment(), true);
                } else {
                    if (result.getForumComment().size() > 0) {
                        refresh.finishLoadMore(true);
                        mDynamicAdapter.addAll(result.getForumComment());
                    } else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;

                ImageUtil.setImage(civPersonalHeadImage, (String) result.getForum().getForum_UserImg());
                tvName.setText(result.getForum().getForum_UserName());
                tvTime.setText(result.getForum().getForum_CreateTime());
                tvDynamicDetail.setText(result.getForum().getForum_Content());
                tvMsg.setText(result.getForum().getForum_CommentNum() + "");
                tvLike.setText(result.getForum().getForum_PraiseNum() + "");

                //   mDynamicAdapter.replaceAll(result.getForumComment(), true);
                if (!TextUtils.isEmpty(result.getForum().getForum_Img()) && result.getForum().getForum_Img().contains(",")) {
                    List<String> list = Arrays.asList(result.getForum().getForum_Img().split(","));
                    if (list.size() > 0) {
                        FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(DynamicDetailsActivity.this, list);
                        rvDynamicPhotos.setAdapter(fansImagesAdpter);
                    }
                }else {
                    if (TextUtils.isEmpty(result.getForum().getForum_Img())) return;
                    List<String> list = Arrays.asList(result.getForum().getForum_Img());
                    FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(DynamicDetailsActivity.this, list);
                    rvDynamicPhotos.setAdapter(fansImagesAdpter);
                }
             //   alreadyLikeList.add(result.getForum().getForum_IsPraise().equals("1"));
                if (!result.getForum().getForum_IsPraise().equals("0")){
                    ivLike.setImageResource(R.mipmap.like1);
                }else {
                    ivLike.setImageResource(R.mipmap.like);
                }
                llLike.setOnClickListener(v -> {
                    Map<String, Object> params1 = new HashMap<>();
                    params1.put("UserID", SPUtil.getString(Constants.USERID));
                    params1.put("ForumID",result.getForum().getForum_ID());
                    //点赞
                    if (!dynaminDetailsBean.getForum().getForum_IsPraise().equals("0")){
                        params1.put("Type", 1 + ""); //取消点赞
                    }else {
                        params1.put("Type",0 + ""); //点赞
                    }

                    HttpClient.get(DynamicDetailsActivity.this, Constants.GIVETHETHUMBSUP, params1,new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result1) {
                            if (!dynaminDetailsBean.getForum().getForum_IsPraise().equals("0")){
                                ivLike.setImageResource(R.mipmap.like);
                                tvLike.setText(Integer.valueOf(tvLike.getText().toString()) - 1 + "");
                                dynaminDetailsBean.getForum().setForum_IsPraise("0");
                            }else {
                                ivLike.setImageResource(R.mipmap.like1);
                                tvLike.setText(Integer.valueOf(tvLike.getText().toString()) + 1 + "");
                                dynaminDetailsBean.getForum().setForum_IsPraise("1");
                            }
                        }
                    });
                });
            }
        });
    }


    @OnClick(R.id.sb_comment)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etSendComment.getText().toString().trim())) {
            Utils.showToastSafe("评论内容不能为空");
            return;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("ForumID", getIntent().getStringExtra("ForumID"));
        params.put("UserID", SPUtil.getString(Constants.USERID));
        if (etSendComment.getHint().toString().contains("回复")) {
            params.put("ParentID", mDynamicAdapter.forumComment_parentID);
        } else {
            params.put("ParentID", "0");
        }
        params.put("Content", etSendComment.getText().toString().trim());
        HttpClient.get(DynamicDetailsActivity.this, Constants.ADDFORUMCOMMENT, params, new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                getForumInformation(true);
                if (etSendComment.getHint().toString().contains("回复")){
                    Utils.showToastSafe("回复评论内容成功");
                }else {
                    Utils.showToastSafe("发表评论内容成功");
                }

                etSendComment.setText("");
                hideSoftKeyboard();
            }
        });
    }


    private void hideSoftKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
